package com.chen.demo.simple.controller;

import com.chen.demo.simple.command.BookAddCommand;
import com.chen.demo.simple.command.BookDeleteCommand;
import com.chen.demo.simple.command.BookUpdateCommand;
import com.chen.demo.simple.command.query.BookPageQuery;
import com.chen.demo.simple.command.query.BookQuery;
import com.chen.demo.simple.domain.entity.Book;
import com.chen.demo.simple.appservice.BookAppService;
import com.chen.winnerframework.http.request.Request;
import com.chen.winnerframework.http.response.MultiResponse;
import com.chen.winnerframework.http.response.PageResponse;
import com.chen.winnerframework.http.response.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
@RequestMapping("/simple/book")
public class BookController {

    private final BookAppService bookAppService;

    @PostMapping
    public Response add(@Valid @RequestBody Request<BookAddCommand> request) {
        return bookAppService.addBook(request.getParams());
    }

    @DeleteMapping
    public Response delete(@Valid BookDeleteCommand command) {
        return bookAppService.deleteBook(command);
    }

    @PutMapping
    public Response update(@Valid @RequestBody Request<BookUpdateCommand> request) {
        return bookAppService.updateBook(request.getParams());
    }

    @GetMapping("/list")
    public MultiResponse<Book> find(BookQuery query) {
        return bookAppService.queryBook(query);
    }

    @GetMapping("/page")
    public PageResponse<Book> page(BookPageQuery pageQuery) {
        return bookAppService.pageQueryBook(pageQuery);
    }

}
